package work;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class test6{
    private static final Object lock = new Object();

    public static void main(String[] args) throws IOException {
		File srcA=new File("fileA.txt");
		File destA=new File("copyA.txt");	
		File srcB=new File("fileB.txt");
		File destB=new File("copyB.txt");		
		File srcC=new File("fileC.txt");	
		File destC=new File("copyC.txt");
    	Thread Thread1 = new Thread(() -> {
                synchronized (lock) {
                	try (FileInputStream fisA = new FileInputStream(srcA); 
                		FileOutputStream fosA = new FileOutputStream(destA)) {
                        byte[] buffer = new byte[1024];
                        int bytesRead;
                        long totalBytesRead = 0;
                        long fileSize = srcA.length();

                        while ((bytesRead = fisA.read(buffer)) != -1) {
                            fosA.write(buffer, 0, bytesRead);
                            totalBytesRead += bytesRead;
                            double progress = ((double) totalBytesRead / fileSize) * 100;
                            System.out.println("A文件复制进度：" + (int) progress + "%");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });


        Thread Thread2 = new Thread(() -> {
                synchronized (lock) {
                	try (FileInputStream fisB = new FileInputStream(srcB); 
                    		FileOutputStream fosB = new FileOutputStream(destB)) {
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            long totalBytesRead = 0;
                            long fileSize = srcB.length();

                            while ((bytesRead = fisB.read(buffer)) != -1) {
                                fosB.write(buffer, 0, bytesRead);
                                totalBytesRead += bytesRead;
                                double progress = ((double) totalBytesRead / fileSize) * 100;
                                System.out.println("B文件复制进度：" + (int) progress + "%");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

        Thread Thread3 = new Thread(() -> {
                synchronized (lock) {
                	try (FileInputStream fisC = new FileInputStream(srcC); 
                    		FileOutputStream fosA = new FileOutputStream(destC)) {
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            long totalBytesRead = 0;
                            long fileSize = srcC.length();

                            while ((bytesRead = fisC.read(buffer)) != -1) {
                                fosA.write(buffer, 0, bytesRead);
                                totalBytesRead += bytesRead;
                                double progress = ((double) totalBytesRead / fileSize) * 100;
                                System.out.println("C文件复制进度：" + (int) progress + "%");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });


        Thread1.start();
        Thread2.start();
        Thread3.start();

    }
}
